package com.xhwl.logistics.schedule;

import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.bo.manipulate.VehicleSyncBO;
import com.xhwl.logistics.component.LookupVehicleParkingLotHandler;
import com.xhwl.logistics.service.business.VehicleAlarmBusinessService;
import com.xhwl.logistics.service.cent.QueryDeviceService;
import com.xhwl.logistics.service.impl.http.CoSonHttpServiceImpl;
import com.xhwl.logistics.service.impl.redis.VehicleIntegratedConfigRedisServiceImpl;
import com.xhwl.logistics.service.impl.redis.VehicleTokenRedisServiceImpl;
import com.xhwl.logistics.strategy.IVehicleParkingLotDeviceHandler;
import com.xhwl.logistics.strategy.IVehicleParkingLotHandler;
import com.xhwl.logistics.strategy.IVehicleParkingLotStatisticsHandler;
import com.xhwl.logistics.strategy.IVehicleParkingLotSyncHandler;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.List;

@Slf4j
//@Service
public class VehicleScheduleService {

    @Autowired
    private QueryDeviceService queryDeviceService;

    @Autowired
    private LookupVehicleParkingLotHandler lookupVehicleParkingLotHandler;

    @Autowired
    private VehicleTokenRedisServiceImpl vehicleTokenRedisService;

    @Autowired
    private VehicleIntegratedConfigRedisServiceImpl vehicleIntegratedConfigRedisService;

    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutorForSchedule;

    @Autowired
    private VehicleAlarmBusinessService vehicleAlarmBusinessService;

    @Autowired
    private CoSonHttpServiceImpl coSonHttpService;

    @XxlJob("logistics-one-hour-after-3-minutes")
    public ReturnT<String> scheduleForOneHourAfter3Minutes(String param) {
        log.info("VehicleScheduleService logistics-one-hour-after-3-minutes start!");

        List<VehicleIntegratedConfigBO> vehicleIntegratedConfigBOList = queryDeviceService.queryIntegratedConfigs();

        for (VehicleIntegratedConfigBO vehicleIntegratedConfigBO : vehicleIntegratedConfigBOList) {
            try {
                Integer productId = vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProductId();

                IVehicleParkingLotHandler vehicleParkingLotHandler = lookupVehicleParkingLotHandler.lookup(productId);
                vehicleParkingLotHandler.statisticsToBi(vehicleIntegratedConfigBO);

                IVehicleParkingLotStatisticsHandler vehicleParkingLotStatisticsHandler = lookupVehicleParkingLotHandler.lookForStatisticsHandler(productId);
                vehicleParkingLotStatisticsHandler.statisticsCardAccessRecordRecordToBi(vehicleIntegratedConfigBO);
                vehicleParkingLotStatisticsHandler.statisticsLocationAccessRecordToBi(vehicleIntegratedConfigBO);
                vehicleParkingLotStatisticsHandler.statisticsCardPayRecordToBi(vehicleIntegratedConfigBO);
            } catch (Exception e) {
                log.error("VehicleScheduleService:scheduleForOneHourAfter3Minutes发生异常错误!错误原因：" + e.getMessage());
            }
        }

        log.info("VehicleScheduleService logistics-one-hour-after-3-minutes end!");
        return ReturnT.SUCCESS;
    }

    @XxlJob("logistics-one-hour-after-30-seconds")
    public ReturnT<String> scheduleForOneHourAfter30Seconds(String param) {
        log.info("VehicleScheduleService logistics-one-hour-after-30-seconds start!");

        List<VehicleIntegratedConfigBO> vehicleIntegratedConfigBOList = queryDeviceService.queryIntegratedConfigs();

        for (VehicleIntegratedConfigBO vehicleIntegratedConfigBO : vehicleIntegratedConfigBOList) {
            try {
                vehicleIntegratedConfigRedisService.insertOrUpdateVehicleIntegratedConfigForProjectId(vehicleIntegratedConfigBO);
                vehicleIntegratedConfigRedisService.insertOrUpdateVehicleIntegratedConfigForParkId(vehicleIntegratedConfigBO);
                vehicleTokenRedisService.insertOrUpdateToken(vehicleIntegratedConfigBO);
            } catch (Exception e) {
                log.error("VehicleScheduleService:scheduleForOneHourAfter30Seconds发生异常错误!错误原因：" + e.getMessage());
            }
        }

        log.info("VehicleScheduleService logistics-one-hour-after-30-seconds end!");
        return ReturnT.SUCCESS;
    }

    @XxlJob("logistics-ten-minutes-after-3-minutes-30-seconds")
    public ReturnT<String> scheduleForTenMinutesAfter3Minutes30Seconds(String param) {
        log.info("VehicleScheduleService logistics-ten-minutes-after-3-minutes-30-seconds start!");

        List<VehicleIntegratedConfigBO> vehicleIntegratedConfigBOList = queryDeviceService.queryIntegratedConfigs();

        for (VehicleIntegratedConfigBO vehicleIntegratedConfigBO : vehicleIntegratedConfigBOList) {
            try {
                Integer productId = vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProductId();

                IVehicleParkingLotStatisticsHandler vehicleParkingLotStatisticsHandler = lookupVehicleParkingLotHandler.lookForStatisticsHandler(productId);
                vehicleParkingLotStatisticsHandler.statisticsCarportRecordToBi(vehicleIntegratedConfigBO);
            } catch (Exception e) {
                log.error("VehicleScheduleService:scheduleForTenMinutesAfter3Minutes30Seconds发生异常错误!错误原因：" + e.getMessage());
            }
        }

        log.info("VehicleScheduleService logistics-ten-minutes-after-3-minutes-30-seconds end!");
        return ReturnT.SUCCESS;
    }

    @XxlJob("logistics-one-day-after-3-hours-15-minutes")
    public ReturnT<String> scheduleForOneDayAfter3Hours5Minutes(String param) {
        log.info("VehicleScheduleService logistics-one-day-after-3-hours-15-minutes start!");

        List<VehicleIntegratedConfigBO> vehicleIntegratedConfigBOList = queryDeviceService.queryIntegratedConfigs();

        for (VehicleIntegratedConfigBO vehicleIntegratedConfigBO : vehicleIntegratedConfigBOList) {
            try {
                Integer projectId = vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId();
                Integer productId = vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProductId();

                VehicleSyncBO vehicleSyncBO = new VehicleSyncBO(projectId);
                IVehicleParkingLotSyncHandler vehicleParkingLotSyncHandler = lookupVehicleParkingLotHandler.lookForSyncHandler(productId);
                vehicleParkingLotSyncHandler.syncBlacklist(vehicleIntegratedConfigBO, vehicleSyncBO);
                vehicleParkingLotSyncHandler.syncCarList(vehicleIntegratedConfigBO, vehicleSyncBO);
                vehicleParkingLotSyncHandler.syncCardRule(vehicleIntegratedConfigBO, vehicleSyncBO);

                IVehicleParkingLotHandler vehicleParkingLotHandler = lookupVehicleParkingLotHandler.lookup(productId);
                threadPoolTaskExecutorForSchedule.execute(() -> {
                    vehicleParkingLotHandler.sync(vehicleIntegratedConfigBO, vehicleSyncBO);
                    vehicleParkingLotHandler.syncDevice(vehicleIntegratedConfigBO);
                });
            } catch (Exception e) {
                log.error("VehicleScheduleService:scheduleForOneDayAfter3Hours5Minutes发生异常错误!错误原因：" + e.getMessage());
            }
        }

        log.info("VehicleScheduleService logistics-one-day-after-3-hours-15-minutes end!");
        return ReturnT.SUCCESS;
    }

    @XxlJob("logistics-five-minutes-after-30-seconds")
    public ReturnT<String> scheduleForVehicleFiveMinutesAfter30Seconds(String param) {
        log.info("VehicleScheduleService start!");

        List<VehicleIntegratedConfigBO> vehicleIntegratedConfigBOList = queryDeviceService.queryIntegratedConfigs();
        for (VehicleIntegratedConfigBO vehicleIntegratedConfigBO : vehicleIntegratedConfigBOList) {
            try {
                Integer productId = vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProductId();
                IVehicleParkingLotDeviceHandler vehicleParkingLotDeviceHandler = lookupVehicleParkingLotHandler.lookForDeviceHandler(productId);
                vehicleParkingLotDeviceHandler.syncDeviceWithStatus(vehicleIntegratedConfigBO);
            }catch (Exception e) {
                log.error("设备定时调度发生异常错误!错误原因：" + e.getMessage());
            }
        }

        log.info("VehicleScheduleService end!");
        return ReturnT.SUCCESS;
    }

}
